package com.gxa.learning.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Certificate;
import com.gxa.learning.pc.service.CertificateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 操作证书的接口
 *
 * @Author ChenJ
 * @date 2021/12/02 16:11
 **/
@RestController
@RequestMapping("/certificate")
@Api(value = "CertificateController", tags = "操作证书的接口")
public class CertificateController {

    @Autowired
    private CertificateService certificateService;

    @PostMapping("/insert")
    @ApiOperation("添加证书的接口")
    @ApiImplicitParam(name = "certificate", value = "certificate", paramType = "body", dataType = "Certificate", dataTypeClass = Certificate.class)
    public Map<String, Object> insert(@RequestBody Certificate certificate) throws BusinessException {

        // 进行数据校验
        if (certificate == null) {
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }

        // 设置当前时间为证书上传时间
        certificate.setTime(new Date());

        String uuid = UUID.randomUUID().toString().replace("-", "");
        certificate.setUuid(uuid);

        Certificate insert = certificateService.insert(certificate);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/select")
    @ApiOperation("获取证书列表的接口（包含模糊查询）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "page", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "size", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "certificate", value = "certificate", dataType = "Certificate", paramType = "body", dataTypeClass = Certificate.class)
    })
    public Map<String, Object> select(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, @RequestBody Certificate certificate) throws BusinessException, IllegalAccessException {

        // 先进行数据校验
        if (certificate == null) {
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 100;
        }

        PageInfo<Certificate> certificatePageInfo = certificateService.selectByPage(page, size, certificate);
        List<Certificate> list = certificatePageInfo.getList();

        // 如果查出没有数据则抛出没有数据异常
        if (list.size() == 0) {
            throw new BusinessException(ExceptionCode.WRONG_DATA);
        }
        long total = certificatePageInfo.getTotal();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(list);
        jsonResult.put("count", total);
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("修改证书的接口")
    @ApiImplicitParam(name = "certificate", value = "certificate", dataType = "Certificate", paramType = "body", dataTypeClass = Certificate.class)
    public Map<String, Object> update(@RequestBody Certificate certificate) throws BusinessException {

        if (certificate == null) {
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }

        Integer integer = certificateService.updateById(certificate);

        if (integer == 0) {
            throw new BusinessException(ExceptionCode.WRONG_UPLOAD);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiOperation("删除证书的接口")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "body")
    @Transactional
    public Map<String, Object> delete(@RequestBody Map<String, Object> id) throws BusinessException {

        if (id == null) {
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }

        // 删除证书操作
        Integer integer = certificateService.deleteById(Long.valueOf(id.get("id").toString()));

        if (integer == 0) {
            throw new BusinessException(ExceptionCode.WRONG_DELETE);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

}
